package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.battle.BattleResult;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.tradewar.CrossUserTradeWarBattle;
import com.yanqu.road.logic.pb.BattlePb;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.pb.tradewar.team.TradeWarTeamProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_TRADE_WAR_BATTLE_START, desc = "跨服商战开战")
public class CrossTradeWarBattleStartCmd extends AbstractCommand {

	private int activityId;

	private BattleResult result;

	private long nowTime;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarBattleStartReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarBattleStartReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		nowTime = System.currentTimeMillis();
		CrossUserTradeWarBattle crossUserTradeWarBattle = player.getModule(TradeWarModule.class).getCrossUserTradeWarBattle();
		if(null == crossUserTradeWarBattle){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(crossUserTradeWarBattle.getEnemyUserId() <= 0){
			player.getModule(TradeWarModule.class).clearCrossTradeWarBattle();
			player.getModule(TradeWarModule.class).syncCrossUserTradeWarBattle();
			ret = GameErrorCode.E_TRADE_WAR_NO_PREPARE;
			return;
		}
		if(crossUserTradeWarBattle.getPatronsList().size() <= 0){
			ret = GameErrorCode.E_TRADE_WAR_NO_PREPARE;
			return;
		}
		if(!TradeWarActivityMgr.canCrossTradeWar(player.getUserId())){
			ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_NO_QUALIFIED;
			return;
		}
		ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo();
		if(!ActivityMgr.activityInTime(activityInfo)){
			ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
			return;
		}
		if(crossUserTradeWarBattle.getEnemyBaseInfo().getServerId() != GameServer.getInstance().getServerId()){
			if(!ServerListMgr.isServerProvideServices(crossUserTradeWarBattle.getEnemyBaseInfo().getServerId())){//对手区服维护
				ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_ENEMY_SERVER_MAINTAIN;
				return;
			}
		}
		result = player.getModule(TradeWarModule.class).crossBattleStart(activityId);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(0 != ret) {
			TradeWarProto.TradeWarBattleStartRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleStartRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_TRADE_WAR_BATTLE_START, respMsg);
		}else {
			if(null != result) {
				TradeWarProto.TradeWarBattleStartRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleStartRespMsg.newBuilder();
				respMsg.setRet(ret);
				if (0 == ret) {
					//战斗详细信息
					respMsg.setBattleDetailList(BattlePb.parseBattleDetailListMsg(result.getPatronsBattleDetailList()));
					//我方出战门客
					if (null != result.getMyPatronsIdList()) {
						respMsg.addAllPatronsIdList(result.getMyPatronsIdList());
					}
					//对手出战门客
					for (UserPatrons userPatrons : result.getEnemyPatronsList()) {
						PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
						if (null != patronsMsg) {
							respMsg.addEnemyPatronsList(patronsMsg);
						}
					}
					respMsg.setKillPatronsCount(result.getKillPatronsCount());
					respMsg.setMyAddScore(result.getMyAddScore());
					respMsg.setEnemyLoseScore(result.getEnemyLoseScore());
					respMsg.setReward(result.getReward());
					respMsg.setEffectMammonSkill(result.getEffectMammonSkill());
					respMsg.setAddEarnSpeed(result.getAddEarnSpeed().intValue());
					respMsg.setUseItemId(result.getUseItemId());
					respMsg.setFastTeamNum(result.getTeamIndex());
				}
				if (result.getTeamIndex() == -1) {
					player.sendPacket(Protocol.U_CROSS_TRADE_WAR_BATTLE_START, respMsg);
				} else {
					CrossTradeWarProto.CrossTradeWarFastBattleRespMsg.Builder builder = CrossTradeWarProto.CrossTradeWarFastBattleRespMsg.newBuilder();
					builder.setRet(0);
					builder.setActivityId(activityId);
					builder.setTeamNum(result.getTeamIndex());

					TradeWarTeamProto.TradeWarTeamBattleTempMsg.Builder resultMsg = TradeWarTeamProto.TradeWarTeamBattleTempMsg.newBuilder();
					UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(result.getEnemyUserId(), GameServer.getInstance().getServerId());//对手基础信息
					if (userBaseInfo != null) {
						resultMsg.setEnemyBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
					} else {
						resultMsg.setEnemyBaseData(PlayerProto.PlayerBaseTempMsg.newBuilder());
					}

					resultMsg.setKillPatronsCount(result.getKillPatronsCount());
					resultMsg.setMyAddScore(result.getMyAddScore());
					resultMsg.setEnemyLoseScore(result.getEnemyLoseScore());
					resultMsg.setReward(result.getReward());
					resultMsg.setEffectMammonSkill(result.getEffectMammonSkill());
					resultMsg.setTeamIndex(result.getTeamIndex());
					resultMsg.setBattleTimes(1);

					builder.setResultMsg(resultMsg);

					player.sendPacket(Protocol.U_CROSS_TRADE_WAR_FAST_BATTLE_START, builder);
				}

				log.info("trade war battle cost time:{}", System.currentTimeMillis() - nowTime);
			}
		}
	}
}
